package utils

import (
	"fmt"
	"regexp"
	"strings"
	"time"
)

//slice翻转
func stringReverse(src []string) {
	count := len(src)
	mid := count/2
	for i := 0;i < mid; i++{
		tmp := src[i]
		src[i] = src[count-1]
		src[count-1] = tmp
		count--
	}
}

//判断是否包含
func SliceStrContains(src []string, value string) bool {
	isContain := false
	for _, srcValue := range src {
		if strings.Contains(value, srcValue) {
			isContain = true
			break
		}
	}

	return isContain
}
func SliceIntContains(src []int, value int) bool {
	isContain := false
	for _, srcValue := range src {
		if srcValue == value {
			isContain = true
			break
		}
	}

	return isContain
}

//判断key是否存在
func MapContains(src map[string]interface{}, key string) bool {
	if _, ok := src[key]; ok {
		return true
	}

	return false
}

//数组去重
func RemoveDuplicateElement(array []string) []string {
	result := make([]string, 0, len(array))
	temp := map[string]struct{}{}
	for _, item := range array {
		if _, ok := temp[item]; !ok {
			temp[item] = struct{}{}
			result = append(result, item)
		}
	}
	return result
}

// Capitalize 字符首字母大写
func ToUpper(str string) string {
	var upperStr string
	vv := []rune(str)   // 后文有介绍
	for i := 0; i < len(vv); i++ {
		if i == 0 {
			if vv[i] >= 97 && vv[i] <= 122 {  // 后文有介绍
				vv[i] -= 32 // string的码表相差32位
				upperStr += string(vv[i])
			} else {
				fmt.Println("Not begins with lowercase letter,")
				return str
			}
		} else {
			upperStr += string(vv[i])
		}
	}
	return upperStr
}

// 根据生日计算年龄
func GetAge(startTime, endTime string) int {
	var Age int64
	var pslTime string
	if strings.Index(startTime, ".") != -1 {
		pslTime = "2006.01.02"
	} else if strings.Index(startTime, "-") != -1 {
		pslTime = "2006-01-02"
	} else {
		pslTime = "2006/01/02"
	}
	t1, err := time.ParseInLocation(pslTime, startTime, time.Local)
	t2, err := time.ParseInLocation("2006-01-02", endTime, time.Local)

	if err == nil && t1.Before(t2) {
		diff := t2.Unix() - t1.Unix()
		Age = diff / (3600 * 365 * 24)
		return int(Age)
	} else {
		return int(Age)
	}
}

// 判断字符串是否为数值
// IsNumeric is_numeric()
// Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part.
// Thus +0123.45e6 is a valid numeric value.
// In PHP hexadecimal (e.g. 0xf4c3b00c) is not supported, but IsNumeric is supported.
func IsNumeric(val interface{}) bool {
	switch val.(type) {
	case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
		return true
	case float32, float64, complex64, complex128:
		return true
	case string:
		str := val.(string)
		if str == "" {
			return false
		}
		// Trim any whitespace
		str = strings.TrimSpace(str)
		if str[0] == '-' || str[0] == '+' {
			if len(str) == 1 {
				return false
			}
			str = str[1:]
		}
		// hex
		if len(str) > 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') {
			for _, h := range str[2:] {
				if !((h >= '0' && h <= '9') || (h >= 'a' && h <= 'f') || (h >= 'A' && h <= 'F')) {
					return false
				}
			}
			return true
		}
		// 0-9, Point, Scientific
		p, s, l := 0, 0, len(str)
		for i, v := range str {
			if v == '.' { // Point
				if p > 0 || s > 0 || i+1 == l {
					return false
				}
				p = i
			} else if v == 'e' || v == 'E' { // Scientific
				if i == 0 || s > 0 || i+1 == l {
					return false
				}
				s = i
			} else if v < '0' || v > '9' {
				return false
			}
		}
		return true
	}

	return false
}

func IsDate(val string) bool {
	// 判断是否为日期
	reg := regexp.MustCompile(`[\d]+`)
	reg2 := regexp.MustCompile(`(?:年|月|-)`)
	reg3 := regexp.MustCompile(`[\p{Han}]+`)
	regStr := reg.FindAllString(val, -1)
	regStr2 := reg2.FindAllString(val, -1)
	regStr3 := reg3.FindAllString(val, -1) //查找所有汉字

	if len(regStr) > 0 && len(regStr2) > 0 {
		if len(regStr3) == 0 && regStr2[0] == "-" {
			return true
		}

		if len(regStr3) > 0 {
			otherHan := SliceDiff(regStr2, regStr3)

			if len(otherHan) > 0 {
				return false
			} else {
				return true
			}
		}
	}

	return false
}

func IsSymbols(val string) bool {
	// 匹配连续的标点符号
	reg := regexp.MustCompile(`[\pP]+`)
	regStr := reg.FindAllString(val, -1)
	reg2 := regexp.MustCompile(`[\p{Han}]+`)
	regStr2 := reg2.FindAllString(val, -1)
	reg3 := regexp.MustCompile(`[\d]+`)
	regStr3 := reg3.FindAllString(val, -1)
	reg4 := regexp.MustCompile(`[\w]+`)
	regStr4 := reg4.FindAllString(val, -1)

	if len(regStr) > 0 && len(regStr2) == 0 &&
		len(regStr3) == 0 && len(regStr4) == 0 {
		return true
	} else {
		return false
	}
}

func SliceDiff(subSlice []string, allSlice []string) []string {
	m := make(map[string]int)
	diffSlice := make([]string, 0)

	for _, v := range subSlice {
		m[v]++
	}

	for _, v := range allSlice {
		val, _ := m[v]
		if val == 0 {
			diffSlice = append(diffSlice, v)
		}
	}

	return diffSlice
}

func substr(s string, pos, length int) string {
	runes := []rune(s)
	l := pos + length
	if l > len(runes) {
		l = len(runes)
	}
	return string(runes[pos:l])
}












